﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;

public enum FontColor { Black, BlackBold, Blue, Grey, Green, GreenBold }

public class DisplayUtil
{
    private static string _space = "&nbsp;";
    private static string _lineBreak = "<br />";
    private static string _NA = "N/A";
    private static string _NONE = "-";

	public DisplayUtil()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void CreateRepeaterForRequestor(Repeater rptList, string strSortby, IList<Ticket> lstTickets, bool isTeam)
    {
        Page pageCurrent = HttpContext.Current.Handler as Page;

        IList<KpmgRepeaterColumnDef> lstCols = new List<KpmgRepeaterColumnDef>();
        IDictionary<string, string> dic = new Dictionary<string, string>();

        dic.Add("AscImage", pageCurrent.ResolveUrl("~/images/sort_asc.gif"));
        dic.Add("DescImage", pageCurrent.ResolveUrl("~/images/sort_desc.gif"));

        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Ticket no", "TicketNo", 8, true));
        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Chargeable", "Chargeable", 16, true));
        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Service type", "ServiceType", 16, true)); ;
        lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Service information", "AdditionalInfo", 24, true)); ;
        if (!isTeam)
        {
            lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Ticket status", "TicketStatus", 12, true));
            lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Date requested", "DateRequested", 9, true));
            lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Action", "Action", 15, true));
        }
        else
        {
            lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Date requested", "DateRequested", 9, true));
            lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Requested by", "RequestedBy", 11, true));
            lstCols.Add(KpmgRepeaterColumnDef.CreateColumnData("Ticket status", "TicketStatus", 12, true));
        }

        string[] arrSortBy = strSortby.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        IList<IDictionary<string, KpmgRepeaterColumnData>> lst = CreateDataForRequestor(lstTickets, pageCurrent, isTeam);
        SortType type = arrSortBy.Length == 2 ? SortType.Str : (SortType)Enum.Parse(typeof(SortType), arrSortBy[2]);
        KpmgRepeater rpt =
            new KpmgRepeater(lstCols, lst, arrSortBy[0],
                arrSortBy[1].ToUpper() == "Asc".ToUpper(),
                dic,
                type);

        rptList.HeaderTemplate = rpt.GetTemplate(ListItemType.Header);
        rptList.ItemTemplate = rpt.GetTemplate(ListItemType.Item);
        rptList.FooterTemplate = rpt.GetTemplate(ListItemType.Footer);

        IList<int> lstData = new List<int>();
        for (int i = 0; i < lst.Count; i++)
            lstData.Add(0);

        rptList.DataSource = lstData;
        rptList.DataBind();
    }
    public static IList<IDictionary<string, KpmgRepeaterColumnData>> CreateDataForRequestor(IList<Ticket> lstTickets, Page pageCurrent, bool isTeam)
    {
        IList<IDictionary<string, KpmgRepeaterColumnData>> lstDataRows = new List<IDictionary<string, KpmgRepeaterColumnData>>();

        foreach (Ticket obj in lstTickets)
        {
            IDictionary<string, KpmgRepeaterColumnData> dicDataRow = new Dictionary<string, KpmgRepeaterColumnData>();

            StringBuilder sbValue = new StringBuilder();
            #region Ticket no
            //string ticketNoValue = isFromKDC ? mail : string.Empty;
            string ticketDetailUrl = pageCurrent.ResolveUrl("~/user/ViewTicket.aspx") + "?ticket_no=" + obj.TicketNo;
            string ticketCopyUrl = pageCurrent.ResolveUrl("~/user/CopyRaisedTicket.aspx") + "?ticket_no=" + obj.TicketNo;
            string copyImage = "<img src='" + pageCurrent.ResolveUrl("~/images/Document.gif") + "'></img>";
            sbValue.Append("<a href='" + ticketDetailUrl + "' class='LinkButton'>" + obj.TicketNo + "</a>");
            sbValue.Append(_lineBreak + _lineBreak + copyImage);
            sbValue.Append("&nbsp;<a id='lblCopy' href='" + ticketCopyUrl + "'>Copy ticket</a>");
            dicDataRow.Add("TicketNo", new KpmgRepeaterColumnData(sbValue.ToString(), obj.TicketNo));
            #endregion

            #region Chargeable
            sbValue.Clear();


            dicDataRow.Add("Chargeable", new KpmgRepeaterColumnData(sbValue.ToString(), obj.ChargeableCode));
            #endregion

            #region Service type
            sbValue.Clear();
            if (sbValue.Length == 0)
                sbValue.Append(_NA);

            dicDataRow.Add("ServiceType", new KpmgRepeaterColumnData(sbValue.ToString(), ""));
            #endregion



            #region Ticket status
            sbValue.Clear();

            dicDataRow.Add("TicketStatus", new KpmgRepeaterColumnData(sbValue.ToString(), ""));
            #endregion

            #region Date requested
            sbValue.Clear();
            //sbValue.Append(obj.RequestedTime.ToString());

            dicDataRow.Add("DateRequested", new KpmgRepeaterColumnData("", ""));
            #endregion

            #region Requested by
            if (isTeam)
            {
                sbValue.Clear();
                dicDataRow.Add("RequestedBy", new KpmgRepeaterColumnData(sbValue.ToString(), sbValue.ToString()));
            }
            #endregion

            #region Action
            if (!isTeam)
            {
                sbValue.Clear();
                string actionImage = String.Empty;
                string actionUrl = String.Empty;
                string actionCommand = String.Empty;
                string actionButton = String.Empty;

                actionUrl = pageCurrent.ResolveUrl("~/user/ViewTicket.aspx") + "?ticket_no=" + obj.TicketNo;

                sbValue.Append(actionImage);
                sbValue.Append("&nbsp;<a href='" + actionUrl + "' class='LinkButton'>" + actionCommand + "</a>");
                dicDataRow.Add("Action", new KpmgRepeaterColumnData(sbValue.ToString(), actionCommand));
            }
            #endregion

            lstDataRows.Add(dicDataRow);
        }

        return lstDataRows;
    }

    public static StringBuilder WithSpan(string content, FontColor color, int startWithBr = 0, int endWitnBr = 0, bool isConvertEmptyToNone = false)
    {
        if (String.IsNullOrEmpty(content) && isConvertEmptyToNone == true)
            content = _NONE;

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < startWithBr; i++)
            result.Append(_lineBreak);
        switch (color)
        {
            case FontColor.Black:
                result.Append("<span class=\"BlackColorFont\">" + content + "</span>");
                break;
            case FontColor.BlackBold:
                result.Append("<span class=\"BlackColorFontBold\">" + content + "</span>");
                break;
            case FontColor.Blue:
                result.Append("<span class=\"BlueColorFont\">" + content + "</span>");
                break;
            case FontColor.Green:
                result.Append("<span class=\"GreenColorFontNormal\">" + content + "</span>");
                break;
            case FontColor.GreenBold:
                result.Append("<span class=\"GreenColorFontBold\">" + content + "</span>");
                break;
            case FontColor.Grey:
                result.Append("<span class=\"GreyColorFont\">" + content + "</span>");
                break;
        }
        for (int i = 0; i < endWitnBr; i++)
            result.Append(_lineBreak);
        return result;
    }

    
}